﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using GG.Business;

namespace GG.Teacher
{
    public partial class EditTaskControl : UserControl
    {
        public EditTaskControl()
        {
            InitializeComponent();

            GenerateGoalFunctionGrid(2);
            GenerateRestrictionsFunctionGrid(2, 2);
        }

        private void GenerateGoalFunctionGrid(int variablesCount)
        {
            this.goalFunctionGrid.Rows.Clear();
            this.goalFunctionGrid.Columns.Clear();

            for (int i = 0; i < variablesCount; i++)
            {
                this.goalFunctionGrid.Columns.Add(i.ToString(), string.Format("x[{0}]", i + 1));
            }

            //this.goalFunctionGrid.Columns.Add("C", "C");

            var column = new DataGridViewComboBoxColumn();
            column.Items.Add("min");
            column.Items.Add("max");
            this.goalFunctionGrid.Columns.Add(column);

            this.goalFunctionGrid.Rows.Add();
        }

        private void GenerateRestrictionsFunctionGrid(int variablesCount, int restrictionsCount)
        {
            this.restrictionsGrid.Rows.Clear();
            this.restrictionsGrid.Columns.Clear();

            for (int i = 0; i < variablesCount; i++)
            {
                this.restrictionsGrid.Columns.Add(i.ToString(), string.Format("x[{0}]", i + 1));
            }

            var column = new DataGridViewComboBoxColumn();
            column.Items.Add("<=");
            column.Items.Add("=");
            column.Items.Add(">=");
            this.restrictionsGrid.Columns.Add(column);

            this.restrictionsGrid.Columns.Add("C", "C");

            for (int i = 0; i < restrictionsCount; i++)
            {
                this.restrictionsGrid.Rows.Add();
            }
        }

        private void nudVariableCount_ValueChanged(object sender, EventArgs e)
        {
            GenerateGoalFunctionGrid((int)this.nudVariableCount.Value);
            GenerateRestrictionsFunctionGrid((int)this.nudVariableCount.Value, (int)this.nudRestrictionsCount.Value);
        }

        private void nudRestrictionsCount_ValueChanged(object sender, EventArgs e)
        {
            GenerateRestrictionsFunctionGrid((int)this.nudVariableCount.Value, (int)this.nudRestrictionsCount.Value);
        }

        public Task GetTask()
        {
            var task = new Task();

            task.Rmax = (int)nudR.Value;

            task.VariablesCount = (int)nudVariableCount.Value;
            foreach (DataGridViewCell cell in goalFunctionGrid.Rows[0].Cells)
            {
                if (cell.ColumnIndex < task.VariablesCount)
                    task.GoalFunctionCoefficients.Add(double.Parse((string)cell.Value));
                if (cell.ColumnIndex == task.VariablesCount)
                //    task.GoalFunctionParam = double.Parse((string)cell.Value);
                //if (cell.ColumnIndex == task.VariablesCount + 1)
                    task.IsMinimized = ((string)cell.Value == "min");
            }

            foreach (DataGridViewRow row in restrictionsGrid.Rows)
            {
                var restriction = new TaskRestriction();

                foreach (DataGridViewCell cell in row.Cells)
                {
                    if (cell.ColumnIndex < task.VariablesCount)
                        restriction.Coefficients.Add(double.Parse((string)cell.Value));
                    if (cell.ColumnIndex == task.VariablesCount)
                        restriction.ComparisonSign = ((string)cell.Value == "<=" ? -1 : (string)cell.Value == "=" ? 0 : 1);
                    if (cell.ColumnIndex == task.VariablesCount + 1)
                        restriction.C = double.Parse((string)cell.Value);
                }

                task.Restrictions.Add(restriction);
            }

            return task;
        }

        public void ShowTask(Task task)
        {
            nudVariableCount.Value = task.VariablesCount;
            nudRestrictionsCount.Value = task.Restrictions.Count;
            nudR.Value = task.Rmax;

            GenerateGoalFunctionGrid(task.VariablesCount);
            GenerateRestrictionsFunctionGrid(task.VariablesCount, task.Restrictions.Count);

            foreach (DataGridViewCell cell in goalFunctionGrid.Rows[0].Cells)
            {
                if (cell.ColumnIndex < task.VariablesCount)
                    cell.Value = task.GoalFunctionCoefficients[cell.ColumnIndex].ToString();
                if (cell.ColumnIndex == task.VariablesCount)
                //    cell.Value = task.GoalFunctionParam.ToString();
                //if (cell.ColumnIndex == task.VariablesCount + 1)
                    cell.Value = task.IsMinimized ? "min" : "max";
            }

            foreach (DataGridViewRow row in restrictionsGrid.Rows)
            {
                var restriction = task.Restrictions[row.Index];
                foreach (DataGridViewCell cell in row.Cells)
                {
                    if (cell.ColumnIndex < task.VariablesCount)
                        cell.Value = restriction.Coefficients[cell.ColumnIndex].ToString();
                    if (cell.ColumnIndex == task.VariablesCount)
                        cell.Value = restriction.ComparisonSign < 0 ? "<=" : restriction.ComparisonSign ==0 ? "=" : ">=";
                    if (cell.ColumnIndex == task.VariablesCount + 1)
                        cell.Value = restriction.C.ToString();
                }
            }
        }
    }
}
